package cn.iocoder.yudao.module.squaredancing.service.version;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.squaredancing.controller.admin.version.vo.*;
import cn.iocoder.yudao.module.squaredancing.dal.dataobject.version.VersionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.squaredancing.dal.mysql.version.VersionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.squaredancing.enums.ErrorCodeConstants.*;

/**
 * 版本管理 Service 实现类
 *
 * @author 赵云安
 */
@Service
@Validated
public class VersionServiceImpl implements VersionService {

    @Resource
    private VersionMapper versionMapper;

    @Override
    public Long createVersion(VersionSaveReqVO createReqVO) {
        // 插入
        VersionDO version = BeanUtils.toBean(createReqVO, VersionDO.class);
        versionMapper.insert(version);

        // 返回
        return version.getId();
    }

    @Override
    public void updateVersion(VersionSaveReqVO updateReqVO) {
        // 校验存在
        validateVersionExists(updateReqVO.getId());
        // 更新
        VersionDO updateObj = BeanUtils.toBean(updateReqVO, VersionDO.class);
        versionMapper.updateById(updateObj);
    }

    @Override
    public void deleteVersion(Long id) {
        // 校验存在
        validateVersionExists(id);
        // 删除
        versionMapper.deleteById(id);
    }

    @Override
        public void deleteVersionListByIds(List<Long> ids) {
        // 删除
        versionMapper.deleteByIds(ids);
        }


    private void validateVersionExists(Long id) {
        if (versionMapper.selectById(id) == null) {
            throw exception(VERSION_NOT_EXISTS);
        }
    }

    @Override
    public VersionDO getVersion(Long id) {
        return versionMapper.selectById(id);
    }

    @Override
    public PageResult<VersionDO> getVersionPage(VersionPageReqVO pageReqVO) {
        return versionMapper.selectPage(pageReqVO);
    }

}